package com.daisuxia.back.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daisuxia.back.dao.IMmanUserLoanDao;
import com.daisuxia.back.service.TaskJobMiddleService;
import com.daisuxia.back.utils.BackConstant;
import com.daisuxia.web.pojo.MmanUserLoan;
import com.daisuxia.web.util.DateUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;



@Service
public class DealOverdueLoanService {
    private static Logger loger = Logger.getLogger(DealOverdueLoanService.class);
    @Autowired
    private TaskJobMiddleService taskJobMiddleService;
    @Autowired
    private IMmanUserLoanDao mmanUserLoanDao; 
    private Lock lock = new ReentrantLock(); 
    
    public void dealOverdueLoan() {
        loger.error("处理预期升级订单 开始" + DateUtil.getDateFormat(new Date(), "yyyy-MM-dd hh:mm:ss"));
        MmanUserLoan mmanUserLoan = new MmanUserLoan();
		mmanUserLoan.setLoanStatus(BackConstant.CREDITLOANAPPLY_OVERDUE);
		List<MmanUserLoan> overdueList = Collections.synchronizedList(new ArrayList<MmanUserLoan>()); 
		overdueList = mmanUserLoanDao.findMmanUserLoanList2(mmanUserLoan);
        ThreadPoolDealOverdueLoan pool = ThreadPoolDealOverdueLoan.getInstance();
//        lock.lock();   // 加锁
        int count = 0;
        for (MmanUserLoan loan : overdueList) {
        	synchronized(this)
        	{
	        	DealOverdueLoanThread dealOverdueOrderThread = new DealOverdueLoanThread(loan.getId(),taskJobMiddleService);
//	        	overdueList.remove(loan);
	        	pool.execute(dealOverdueOrderThread);
	        	count ++;
        	}
        	try {
        		Thread.sleep(10);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}finally{
//        		lock.unlock();  // 释放锁
        	}
		}
        System.out.println("count............."+count);
    	
//    	System.out.println("test------------------------------");
    }
    
    
    public static void main(String[] args) {
    	new DealOverdueLoanService().dealOverdueLoan();
	}
}
